<template>
  <div class="my-custom">
    <div>
      <div class="tabs">
        <div
          class="tab-item"
          v-for="(item, index) of tabs"
          :key="index"
          @click="tabItemClick(index)"
          :class="{'active': activeIndex === index}"
        >{{item}}</div>
        <div class="tab-angle" :style="{left: anLeft +'rem'}">
          <img src="@/assets/images/icon_sjb.png" alt />
        </div>
      </div>
      <div class="sec-tabs" v-if="activeIndex === 1">
        <div
          class="tab-item"
          v-for="(item, index) of secTabs"
          :key="index"
          @click="secTabItemClick(index)"
          :class="{'active': secActiveIndex === index}"
        >{{item}}</div>
        <div class="tab-angle" :style="{left: secAnLeft +'rem'}">
          <img src="@/assets/images/icon_sjb.png" alt />
        </div>
      </div>
      <Scroll
        class="custom-scroll"
        :style="{top: activeIndex ? '2rem' : '1rem', background: activeIndex ? '#f7f7f7' : '#fff'}"
        ref="scroller"
        :listenScroll="true"
        :pullUp="true"
        :probeType="3"
        @scrollToEnd="handleScrollToEnd"
        :data="customList"
      >
        <div>
          <div class="content" v-if="activeIndex === 0">
            <div v-for="(item, index) of customList" :key="index" class="custom-item">
              <panel-custom
                @customEdit="$router.push({path: '/my/my-custom/custom-detail', query: {id: item.id}})"
                :customInfo="item"
              />
            </div>
          </div>
          <div v-if="activeIndex === 1">
            <div class="order-list" v-if="orderList.length && shops">
              <div class="panel-order" v-for="(item, index) of orderList" :key="index">
                <div class="top">
                  <div class="left">
                    <p class="name">{{ item.customers_name }}</p>
                    <p class="tel">{{ item.customers_phone ? item.customers_phone : '未填写' }}</p>
                  </div>
                  <div
                    class="right"
                  >{{item.status === 0 ? '待审批' : item.status === 1 ? '已同意' : '已拒绝'}}</div>
                </div>
                <div class="bottom">
                  <p class="time">时间：{{ item.created_at }}</p>
                  <p class="brand">
                    {{ getShopName(item.shop_id)}}
                    <span>|</span>
                    {{item.brand_name}}
                  </p>
                  <p class="price">消费金额：{{parseFloat(item.price) ? item.price : '--'}}</p>
                </div>
              </div>
            </div>
          </div>
        </div>
      </Scroll>
      <no-result
        v-if="!orderList.length && activeIndex === 1"
        :src="require('@/assets/images/1-zwnr.png')"
        msg="暂无订单~"
      />
      <no-result
        v-if="!customList.length && activeIndex === 0"
        :src="require('@/assets/images/2-zwnr.png')"
        msg="暂无客户~"
      />
    </div>
    <div
      class="add-custom"
      v-if="activeIndex === 0"
      @click="$router.push('/my/my-custom/add-custom')"
    ></div>
    <transition name="slide">
      <router-view @success="handleSuccess"></router-view>
    </transition>
  </div>
</template>
<script>
import NoResult from '@/components/no-result/no-result'
import PanelCustom from '@/components/panel-custom'
import Scroll from '@/components/scroll/scroll'
import { getCustomOrderList } from '@/api/my'
import CostomList from '@/views/my/mixins/costom-list'
import ConfigList from '@/views/my/mixins/config-list'
const ERR_OK = 200
export default {
  components: {
    NoResult,
    PanelCustom,
    Scroll
  },
  mixins: [ConfigList, CostomList],
  data () {
    return {
      tabs: ['我的客户', '全部订单'],
      secTabs: ['待审核', '已同意', '未通过'],
      activeIndex: 0,
      anLeft: 0.85,
      secActiveIndex: 0,
      secAnLeft: 0.7,
      page: 1,
      status: 0,
      orderList: [],
      total: 0,
      loading: true
    }
  },
  methods: {
    handleScrollToEnd () {
      if (this.activeIndex && this.loading) {
        this.page++
        this._getCustomOrderList({ status: this.status, page: this.page }).then(
          res => {
            this.$notify({
              type: 'success',
              message: `已为您更新${res.data.data.length}条数据`,
              className: 'my-cls',
              duration: 1000
            })
          }
        )
      } else if (this.activeIndex && !this.loading) {
        this.$notify({
          type: 'warning',
          message: '没有更多内容了~',
          className: 'my-cls',
          duration: 1000
        })
      }
    },
    handleSuccess () {
      this._getMyCustomList()
    },
    tabItemClick (index) {
      this.activeIndex = index
      this.$refs.scroller.refresh()
      this.$refs.scroller.scrollTo(0, 0)
      this.orderList = []
      this.page = 1
      switch (index) {
        case 0:
          this.anLeft = 0.85
          this._getMyCustomList()
          break
        case 1:
          this.anLeft = 2.7
          this._getCustomOrderList({
            status: this.status,
            page: this.page
          })
          break
      }
    },
    secTabItemClick (index) {
      this.secActiveIndex = index
      this.orderList = []
      this.page = 1
      this.$refs.scroller.scrollTo(0, 0)
      switch (index) {
        case 0:
          this.secAnLeft = 0.7
          this.status = 0
          this._getCustomOrderList({ status: this.status, page: this.page })
          break
        case 1:
          this.status = 1
          this.secAnLeft = 2.25
          this._getCustomOrderList({ status: this.status, page: this.page })
          break
        case 2:
          this.status = -1
          this.secAnLeft = 3.8
          this._getCustomOrderList({ status: this.status, page: this.page })
          break
      }
    },
    hasMore () {
      if (this.orderList.length >= this.total) {
        this.loading = false
      } else {
        this.loading = true
      }
    },

    _getCustomOrderList ({ status, page }) {
      return getCustomOrderList({ status, page }).then(res => {
        if (res.code === ERR_OK) {
          console.log(res)
          this.orderList = this.orderList.concat(res.data.data)
          this.total = res.data.total
          this.hasMore()
          return Promise.resolve(res)
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
@import "@/styles/mixin.scss";
.my-custom {
  position: fixed;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  z-index: 9;
  background: #fff;
  touch-action: none;
  .custom-scroll {
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    overflow: hidden;
  }
  .tabs,
  .sec-tabs {
    width: 100%;
    height: 1rem;
    background: rgba(255, 255, 255, 1);
    padding: 0 0.36rem;
    box-sizing: border-box;
    display: flex;
    justify-content: flex-start;
    position: relative;
    .tab-item {
      font-size: 0.28rem;
      line-height: 0.28rem;
      font-family: PingFang SC;
      font-weight: 300;
      color: rgba(51, 51, 51, 1);
      text-align: center;
      margin-right: 0.71rem;
      padding: 0.36rem 0;
      &.active {
        font-weight: bold;
      }
    }
    .tab-angle {
      position: absolute;
      bottom: 0;
      width: 0.12rem;
      height: 0.06rem;
      transition: all 0.15s;
      img {
        width: 100%;
        vertical-align: top;
      }
    }
  }
  .sec-tabs {
    border-top: 1px solid rgba(0, 3, 0, 0.1);
  }
  .content {
    width: 100%;
    padding: 0.4rem 0.36rem;
    box-sizing: border-box;
  }

  .add-custom {
    width: 0.68rem;
    height: 0.68rem;
    background-image: url("../../assets/images/add.png");
    background-repeat: no-repeat;
    background-size: 0.42rem 0.42rem;
    background-position: center center;
    background-color: #000;
    border-radius: 50%;
    overflow: hidden;
    position: fixed;
    right: 0.36rem;
    bottom: 3.11rem;
  }
  .order-list {
    width: 100%;
    padding: 0.6rem 0.36rem;
    box-sizing: border-box;
    .panel-order {
      width: 100%;
      background: #fff;
      margin-bottom: 0.4rem;
      .top {
        display: flex;
        justify-content: space-between;
        padding: 0.6rem 0.4rem 0.4rem 0.4rem;
        box-sizing: border-box;
        border-bottom: 1px dashed rgba(0, 4, 0, 0.3);
        .left {
          .name {
            font-size: 0.28rem;
            font-family: PingFang SC;
            font-weight: 500;
            color: rgba(4, 0, 0, 1);
          }
          .tel {
            font-size: 0.22rem;
            font-family: PingFang SC;
            font-weight: 400;
            color: rgba(0, 4, 0, 1);
            line-height: 0.32rem;
            margin-top: 0.2rem;
          }
        }
        .right {
          font-size: 0.26rem;
          font-family: PingFang SC;
          font-weight: 600;
          color: rgba(4, 0, 0, 1);
        }
      }
      .bottom {
        width: 100%;
        padding: 0.4rem 0.4rem 0.6rem 0.4rem;
        box-sizing: border-box;
        .time {
          font-size: 0.26rem;
          font-family: PingFang SC;
          font-weight: 400;
          color: rgba(102, 102, 102, 1);
        }
        .brand,
        .price {
          font-size: 0.26rem;
          font-family: PingFang SC;
          font-weight: 400;
          color: rgba(4, 0, 0, 1);
          margin-top: 0.4rem;
          span {
            padding: 0 0.1rem;
          }
        }
      }
    }
  }
}
</style>
